package com.pine.common.database.constant;

import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.pine.common.database.interceptor.CustomDataChangeRecorderInnerInterceptor;
import com.pine.common.database.interceptor.MybatisPlusPermissionInterceptor;
import com.pine.common.database.interceptor.MybatisPlusSensitiveInterceptor;
import com.pine.common.database.interceptor.MysqlPaginationInnerInterceptor;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.lang.reflect.InvocationTargetException;

/**
 * 支持的mybatis-plus-inner拦截器枚举
 *
 * @author pine manage
 * @since 2024-08-09
 */
@Getter
@AllArgsConstructor
public enum SupportedInnerInterceptorEnum {
    OPTIMISTIC_LOCKER_INNER_INTERCEPTOR(OptimisticLockerInnerInterceptor.class, "乐观锁拦截器"),
    ILLEGAL_SQL_INNER_INTERCEPTOR(IllegalSQLInnerInterceptor.class, "非法SQL拦截器"),
    DATA_CHANGE_RECORDER_INNER_INTERCEPTOR(CustomDataChangeRecorderInnerInterceptor.class, "数据变更记录拦截器"),
    BLOCK_ATTACK_INNER_INTERCEPTOR(BlockAttackInnerInterceptor.class, "防全表更新与删除拦截器"),
    MYSQL_PAGINATION_INNER_INTERCEPTOR(MysqlPaginationInnerInterceptor.class, "MySQL分页拦截器"),
    DATA_PERMISSION_INTERCEPTOR(MybatisPlusPermissionInterceptor.class, "数据权限拦截器"),
    MYBATIS_PLUS_SENSITIVE_INTERCEPTOR(MybatisPlusSensitiveInterceptor.class, "数据加密拦截器"),
    ;


    private final Class<? extends InnerInterceptor> interceptorClass;
    private final String desc;

    /**
     * 获取拦截器
     */
    public InnerInterceptor getInterceptor() {
        try {
            return interceptorClass.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
